<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace Admin\Controller;

/**
 * Description of AmazonSkuController
 *
 * @author zhouyehao
 */
class AmazonSkuController extends AdminController {

    /**
     * 列表
     */
    public function index() {
        $where = array();
        //日期选择
        $seller_sku = I("seller_sku");
        if (!empty($seller_sku)) {
            $where['ms.seller_sku'] = array('like', '%' . $seller_sku . '%');
            $this->assign("seller_sku", $seller_sku);
        }
//
//        $amazon_order_id = I("amazon_order_id");
//        if (!empty($amazon_order_id)) {
//            $where['mo.amazon_order_id'] = $amazon_order_id;
//            $this->assign("amazon_order_id", $amazon_order_id);
//        }
        //日期选择
        $start_date = I("start_date");
        $end_date = I("end_date");

        if (!empty($end_date) && !empty($start_date)) {
            if ($end_date <= $start_date) {
                $this->error('开始时间不允许小于等于结束时间');
                exit;
            } else {
                $where['ms.update_time'] = array("between", array($start_date, $end_date . ' 23:59:59'));
            }
            $this->assign("start_date", $start_date);
            $this->assign("end_date", $end_date);
        } else {
            if (!empty($start_date)) {
                $where['ms.update_time'] = array('EGT', $start_date);
                $this->assign("start_date", $start_date);
            }

            if (!empty($end_date)) {
                $where['ms.update_time'] = array('ELT', $end_date . ' 23:59:59');
                $this->assign("end_date", $end_date);
            }
        }

        $M = M()->table("lt_mws_sku ms");

        $list = $this->lists($M, $where, 'ms.create_time desc,ms.update_time desc');

//        $test = ['ID','SKU','ASIN','FNSKU','总库存','在途库存','创建时间','更新时间'];
//        exportExcel('test',$test , $list);

        $this->assign('_list', $list);
        $this->meta_title = '亚马逊SKU列表';
        $this->display();
    }

    public function importsku() {
        if (IS_POST) {

//            dump($_FILES);die;

            /* 调用文件上传组件上传文件 */
            $Picture = D('File');
            $info = $Picture->upload(
                    $_FILES, C('EXCEL_UPLOAD'), C('PICTURE_UPLOAD_DRIVER'), C("UPLOAD_LOCAL_CONFIG")
            ); //TODO:上传到远程服务器

            /* 记录图片信息 */
            if ($info) {
                $return['status'] = 1;
                $return = array_merge($info['download'], $return);
            } else {
                $return['status'] = 0;
                $return['info'] = $Picture->getError();
            }

//                        dump($info);
//            die;
             $file_name = './Uploads/Excel/2017-11-26/5a1ab20377860.xls';
//            $file_name = __ROOT__.$info['importsku']['path'];


            $result = importSku($file_name);
            var_dump($result);
            die;
        } else {
            $this->display();
        }
    }

}
